#include <iostream>

#include "SQLliteWrapper.hpp"

int main()
{
    try
    {
        // 打开数据库
        SQLite::Database db("example.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);

        // 创建一个表
        db.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)");

        // 插入数据
        SQLite::Statement insert(db, "INSERT INTO users (name) VALUES (?)");
        insert.bind(1, "John");
        insert.exec();

        // 查询数据
        SQLite::Statement query(db, "SELECT * FROM users");
        while (query.executeStep())
        {
            std::cout << "ID: " << query.getColumn(0) << ", Name: " << query.getColumn(1) << std::endl;
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    try
    {
        // 打开数据库
        SQLite::Database db("example1.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);

        DatabaseShow(db);
        // 创建 users 表
        db.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)");
        db.exec("INSERT INTO users (id, name) VALUES (1, 'Alice'), (2, 'Bob')");

        // 创建 orders 表
        db.exec("CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, user_id INTEGER, description TEXT)");
        db.exec("INSERT INTO orders (id, user_id, description) VALUES (1, 1, 'Order 1'), (2, 1, 'Order 2'), (3, 2, 'Order 3')");

        // 执行 JOIN 查询，连接 users 和 orders 表
        SQLite::Statement query(db, "SELECT users.name, orders.description "
                                    "FROM users "
                                    "JOIN orders ON users.id = orders.user_id");

        // 处理查询结果
        while (query.executeStep())
        {
            std::cout << "User: " << query.getColumn(0) << ", Order: " << query.getColumn(1) << std::endl;
        }
        DatabaseShow(db);
    }
    catch (const std::exception &e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}
